<?php
namespace app\home\controller;
use think\Controller;
use think\Session;
use think\Db;
use think\Request;
use think\paginator;
use think\File;
use think\filter;
class Center extends Controller{

    //个人中心首页
    public function index(){
        //检测登录

        //个人中心首页
        $id = Session::get('USERS.user_id');
        $row = Db::table('users')->where("user_id = $id")->find();
        $this->assign("row",$row);
        return view();

    }
    //信息修改
    public function userInfo(){
        $param = Request::instance()->get();
        $id = $param['id'];
        $row = Db::table('users')->where("user_id = $id")->find();
        $this->assign("row",$row);
        return view();
    }

    /**
     *我的收藏
     */
    public function cOllec(){
        $id = Session::get("USERS.user_id");
//        $row = Db::table('collect')->where("user_id = $id")->select();
        $row = Db::table('collect')
            ->alias("co")
            ->where('currentUser_id',$id)
            ->join('courses cou','collect.courses_id = courses.courses_id')
            ->select();

        $this->assign("row",$row);
        return view();
    }

    /*
     * 收藏删除
     */
    public function collecDel(){

        $param = Request::instance()->get();
        $collect_id = $param['id'];

        $row = Db::table('collect')->where("collect_id",$collect_id)->delete();

        echo json_encode($row);
        exit;

    }

    /*
     * 上传头像
     */
    public function userPhoto(){

        $request = Request::instance()->post();

        $data = array();
        $data['user_id'] = Session::get('USERS.user_id');
        $data['user_photo'] = $request['user_photo'];

        $res = Db::table('users')->update($data);

        echo json_encode($res);
        exit;

    }

    /*
     * 修改个人信息
     */
    public function personalInfo(){

        $request = Request::instance()->post();

        $data = array();
        $data['user_id'] = Session::get('USERS.user_id');
        $data['user_name'] = $request['user_name'];
        $data['user_sex'] = $request['user_sex'];
        $data['user_age'] = $request['user_age'];
        $data['user_email'] = $request['user_email'];

        $res = Db::table('users')->update($data);
        echo json_encode($res);

    }

    /*
     *我的课程
     */
    public function myCourse(){
        $id = Session::get("USERS.user_id");
        $lv = Session::get('USERS.user_grade');
        $res = Db::table('courses')
            ->where('user_id',$id)
            ->where("courses_del",2)
            ->select();

        $row = Db::table('courses')
            ->where('user_id',$id)
            ->where("courses_del",0)
            ->select();

        $this->assign('lv',$lv);
        $this->assign("res",$res);
        $this->assign("row",$row);
        return $this->fetch();
    }

    /*
     * 我的课程删除
     */
    public function coursesDel(){
        $request = Request::instance()->get();

        $courses_id = $request['id'];
        $row = Db::table('courses')->where("courses_id",$courses_id)->delete();

        echo json_encode($row);
    }

    /*
     * 上传课程
     */
    public function uploadCourse(){

         //课程分类
        $res = Db::table("courseclassify")->where("classify_del",0)->select();
        $this->assign("res",$res);
        return $this->fetch();

    }

    /*
     *上传课程
     */
    public function addCourse(){
        $param = Request::instance()->post();
        $data =array();
        $data['courses_photo'] = $param['courses_photo'];
        $data['courses_name'] = $param['courses_name'];
        $data['courses_title'] = $param['courses_title'];
        $data['courses_money'] = $param['courses_money'];
        $data['courses_content'] = $param['courses_content'];
        $data['classify_id'] = $param['classify_id'];
        $data['user_id'] = Session::get("USERS.user_id");
        $data['courses_del'] = 2;
        $data['courses_time'] = time();

        if($data['user_id'] == ""){
            echo json_encode(0);
            exit;
        }


        $res = Db::table("courses")->insert($data);

        echo json_encode($res);
        exit;
    }


    /**
     *积分兑换
     */
    public function pointsList(){

        $param = Request::instance()->get();

        $id = Session::get('USERS.user_id');

        $row = Db::table('score')->where("user_id = $id")->select();

        $res=Db::table('users')->where("user_id = $id")->find();

        $this->assign('res',$res);

        $this->assign("row",$row);

        return view();
    }




    /**
    *账户安全
     */
    public function accountsecurity(){
        //加载账户安全
        $param = Request::instance()->get();
        $id = $param['id'];
        $row = Db::table('users')->where("user_id = $id")->find();
        $this->assign("row",$row);
        return view();
    }



//    显示密码修改
    public function updatepwd(){


          return $this->fetch();

         }
    //密码修改
    public function password(){

        $param = Request::instance()->post();

        $id = Session::get('USERS.user_id');


        $old_password=$param['old_password'];
        $user_pwd=$param['user_pwd'];
        $user_pwd1=$param['user_pwd1'];


        $row = Db::table('users')->where("user_id = $id")->find();



        if(empty($old_password) || empty($user_pwd) || empty($user_pwd1)){
            echo 4;
            exit;
        }

        if($row['user_pwd']!=md5(md5($old_password).$row['user_no'])){
               echo 1;
               exit;
        }


        if($user_pwd!=$user_pwd1){
            echo 2;
            exit;
        }


//        $rand_str = _rand_str(4);
        $rand_str = $row['user_no'];


        $user_model = new \app\home\model\Center();

        $register = $user_model->update_password($user_pwd,$rand_str,$id);

          if($register!=false){
              Session::clear();
              echo 0;
              exit;
          }else{
              echo 3;
              exit;
          }
    }


    /**
     *财富中心
     */
    public function wealth(){
        $param = Request::instance()->get();
        $id = $param['id'];
        $row = Db::table('users')->where("user_id = $id")->find();
        $this->assign("row",$row);
        return view();
    }

    //我的余额
    public function account(){
        $param = Request::instance()->get();
        $id = $param['id'];
        $row = Db::table('users')->where("user_id = $id")->find();
        $this->assign("row",$row);
        return view();
    }


    /**
     *设置
     */
    public function site(){
        $param = Request::instance()->get();
        $id = $param['id'];
        $row = Db::table('users')->where("user_id = $id")->find();
        $this->assign("row",$row);
        return view();

    }

    //退出
    public function login_out(){

        Session::clear();

        echo json_encode(1);
        exit;

    }

    /*
     *
     * 文章详情
     * */
    public function public_articles(){

           $id = Session::get('USERS.user_id');
            $article= Db::table('article')->where("user_id=$id")->order('article_time desc')->paginate(2);
            $page = $article->render();
            $this->assign('article',$article);
            $this->view->assign('page', $page);

          return $this->fetch();
    }
    /*
     * 显示文章
     *
     * */
    public function view_article(){

        $userSession = Session::get("USERS");
        $this->view->assign("user_id",$userSession['user_id']);

        $catname=Db::table('articlecats')->select();
        $this->assign('catname',$catname);


        return $this->fetch('insert_articles');

}
    /*
     * 发布文章
     * */

    public function insert_articles(){




        $info=Request::instance()->post();

        $str=$info['article_content'];

        $data['article_content'] = strip_tags($str);

        $data['article_title']=$info['article_title'];

        $data['photo']=$info['photos'];

        $data['pid'] =$info['catname'];
        $data['user_id'] = Session::get('USERS.user_id');
        $data['article_time'] = time();

//               $filter = new \filter();
//
//               $get_file = $filter->clean($article_content);


        if(empty($data['article_content']) || empty($data['article_title']) || empty($data['photo'])){
            echo 2;
            exit;
        }


        $model= new \app\home\model\Center();


        $insert=$model->save_article($data);

        if($insert > 0){
            echo 0;
            exit;
        }else{
            echo 1;
            exit;
        }

    }
     /*
      * 图片上传
      * */
    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('photo');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size'=>1150678,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/photo');
        if($info){
            // 成功上传后 获取上传信息
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $res = $info->getSaveName();
        }else{
            // 上传失败获取错误信息
            $res = 1;
        }

        echo json_encode($res);
    }

    
    //视频上传
    public function video_Upload(){
        $video = request()->file("video_Upload");
        $info = $video->validate(['size'=>1024*100000000000,'ext'=>'mp4,avi,wmv'])->move(ROOT_PATH . 'public' . DS . 'uploads/media');
        if($info){
            // 成功上传后 获取上传信息
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $str = $info->getSaveName();

        }else{
            // 上传失败获取错误信息
            $str = 1;
        }
        echo json_encode($str);
    }

    /*
     * 团队介绍
     * */
     public function team_introduction(){

         return $this->fetch();
     }

     /*
      * 删除文章
      * */
    public function articles_delete(){


        $res=Request::instance()->get();

        $id=$res['id'];

       $rew=  Db::table('article')->where("article_id=$id")->delete();

       if($rew>0){
           echo 1;
       }else{
           echo 2;
       }
    }
}